﻿using msBsc.AppLogic.DataAlc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.ShpMana
{
    public partial class wfAddRouteDatail : msBsc.Cls.BPageHt
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //保存数据
        public void SaveRouteDatail()
        {
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.alert('登录时间过期，请重新登录.', 3);");
                return;
            }
            string routeid = this.GetParamHtValue("routeid");
            if (string.IsNullOrEmpty(routeid))
            {
                this.ExecJScript(@"layer.alert('该路线不存在.', 3);");
                return;
            }
            string routName = this.GetParamHtValue("routName");
            if (string.IsNullOrEmpty(routName))
            {
                this.ExecJScript(@"layer.alert('请填写路线名称.', 3);");
                return;
            }
            string routeDetail = this.GetParamHtValue("detailids");
            if (string.IsNullOrEmpty(routeDetail))
            {
                this.ExecJScript(@"layer.alert('请选择景点名称.', 3);");
                return;
            }

            string day = this.GetParamHtValue("day");
            if (string.IsNullOrEmpty(day))
            {
                this.ExecJScript(@"layer.alert('请选择天数.', 3);");
                return;
            }

            using (CBizDjkRouteDetail detail = new CBizDjkRouteDetail())
            {
                string status = detail.AddRouteDetail(routeid, routName, routeDetail, "1", Convert.ToInt32(day), userid);

                if (status == "OK")
                {
                    this.ExecJScript(@"success();");
                    return;
                }

                this.ExecJScript(@"layer.alert('" + status + "', 3);");
            }

        }
        //加载景区
        public void GetPushSenic()
        {
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.alert('登录时间过期，请重新登录.', 3);");
                return;
            }
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");
            string destId = this.GetParamHtValue("destinationId");
            string scenic = this.GetParamHtValue("scenic");
            string pageIndex = this.GetParamHtValue("pageIndex");

            using (CBizPushScenic pushScenic = new CBizPushScenic())
            {
                DataSet ds = pushScenic.GetRouteDatailePageList(scenic, countryId, cityId, destId, 1, 24, Convert.ToInt32(pageIndex));
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    this.ExecJScript(@"$('#sceic_view_list').html('没有任何数据...');");
                    return;
                }

                var countryJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 24, "");

                this.ExecJScript(@"roadHtml(" + countryJson + "," + ds.Tables[0].Rows[0]["sumCount"].ToString() + "," + 24 + "," + pageIndex + ")");
            }
        }

        //加载 路线
        public void LoadRouteBaseInfo()
        {
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.alert('登录时间过期，请重新登录.', 3);");
                return;
            }

            string routeid = this.GetParamHtValue("routeid");

            using (CBizDjkRouteBaseinfo routinfo = new CBizDjkRouteBaseinfo())
            {
                DataSet ds = routinfo.GetRouteInfoByID(routeid);

                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    this.ExecJScript(@"layer.alert('该路线不存在.', 3);");
                    return;
                }
                this.ExecJScript("setRoutInfo('" + ds.Tables[0].Rows[0]["ROUTE_BASEINFO_NME"].ToString() + "'," + ds.Tables[0].Rows[0]["ROUTE_BASEINFO_DAYS"].ToString() + ");");

            }

        }

        public override void DoAction()
        {
            if (this.strAction == "saveRoute")
            {
                SaveRouteDatail();
            }
            else if (this.strAction == "selectDatail")
            {
                GetPushSenic();
            }
            else if (this.strAction == "lodingRout")
            {
                LoadRouteBaseInfo();
            }
            else if (this.strAction == "changeArea")
            {

                RefreshArea();

            }
        }

        private void RefreshArea()
        {
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");

            DataSet ds1, ds2;

            using (msBsc.AppLogic.DataAlc.CBizCountryCity objCBizCountryCity = new AppLogic.DataAlc.CBizCountryCity())
            {
                ds1 = string.IsNullOrEmpty(countryId) ? null : objCBizCountryCity.SelectCountryByParentId(countryId);

                ds2 = string.IsNullOrEmpty(cityId) ? null : objCBizCountryCity.SelectCountryByParentId(cityId);
            }

            if (ds1 != null)
            {
                var countryJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds1.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#country_city_list').html();
                            laytpl(_html).render(" + countryJson + @", function (render) {
                                $('#selCity').html(render);
                            });");
            }

            if (ds2 != null)
            {
                var cityJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds2.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#city_des_list').html();
                            laytpl(_html).render(" + cityJson + @", function (render) {
                                $('#selDestination').html(render);
                            });");
            }
        }
    }
}